package com.gzhryc.shared_device.bxscn.code.services;

import com.gzhryc.common.StringTools;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.shared_device.bxscn.code.dao.PortableEnergyBatteryTypeDao;
import com.gzhryc.shared_device.bxscn.code.dao.db.PortableEnergyBattery;
import com.gzhryc.shared_device.bxscn.code.dao.db.PortableEnergyBatteryType;
import com.gzhryc.shared_device.bxscn.code.services.dto.PortableEnergyBatteryTypeSearch;
import com.gzhryc.shared_device.common.device.dao.db.DeviceType;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class PortableEnergyBatteryTypeService {

    static Logger log = Logger.getLogger(PortableEnergyBatteryTypeService.class);

    public static PortableEnergyBatteryTypeService self() {
        return MultiDBTools.getService(PortableEnergyBatteryTypeService.class, true);
    }

    public static PortableEnergyBatteryTypeService self(String jdbcKey) {
        return MultiDBTools.getService(jdbcKey, PortableEnergyBatteryTypeService.class, true);
    }

    PortableEnergyBatteryTypeDao dao;

    public PortableEnergyBatteryTypeService(String jdbcKey) {
        this.dao = new PortableEnergyBatteryTypeDao(jdbcKey);
    }

    public List<PortableEnergyBatteryType> findBySearch(PortableEnergyBatteryTypeSearch search, Integer pageNo, Integer pageLimit){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.findByCondition(conditions,pageNo,pageLimit);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public Long countBySearch(PortableEnergyBatteryTypeSearch search){
        Conditions conditions = dao.buildConditions(search);
        try {
            return dao.count(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return 0L;
    }

    public List<PortableEnergyBatteryType> findByIds(List<String> ids){
        if(ids != null && ids.size() > 0) {
            Conditions conditions = new Conditions(PortableEnergyBatteryType.class);
            conditions.and().in("id",ids);
            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public PortableEnergyBatteryType getById(String id){
        try {
            return dao.getById(id);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public PortableEnergyBatteryType getByDefault(){
        Conditions conditions = new Conditions(PortableEnergyBatteryType.class);
        conditions.desc("createDate");
        try {
            return dao.get(conditions);
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return null;
    }

    public boolean add(PortableEnergyBatteryType portableEnergyBatteryType) throws LogicException {
        portableEnergyBatteryType.setCreateDate(new Date());
        try {
            return dao.insert(portableEnergyBatteryType) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean updateNotNull(PortableEnergyBatteryType portableEnergyBatteryType) throws LogicException{
        try {
            return dao.updateNotNull(portableEnergyBatteryType) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }

    public boolean deleteById(String id) throws LogicException {
        Long total = PortableEnergyBatteryService.self().countByBatteryType(id);
        if(total > 0){
            throw new LogicException("存在对应设备，不允许删除");
        }
        try {
            return dao.deleteById(id) > 0;
        } catch (SQLException e) {
            log.error(e.getMessage(),e);
        }
        return false;
    }
}
